﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellWorkChain.h"

namespace cell
{

//-----------------------------------------------------------------------
WorkChain::WorkChain(const String& name)
	: mName(name)
	, mCurrentEntry(NULL)
{
	mHeadSection = CELL_NEW HeadSection(this);
	mHead = CELL_NEW WorkChain::Entry(this, NULL, NULL, mHeadSection);

	mTailSection = CELL_NEW TailSection(this);
	mTail = CELL_NEW WorkChain::Entry(this, NULL, NULL, mTailSection);

	// 初始化下一个节点
	mHead->mNextEntry = mTail;
	mTail->mPrevEntry = mHead;
}
//-----------------------------------------------------------------------
WorkChain::~WorkChain(void)
{
	if (mHead)
		CELL_DELETE mHead;
	if (mHeadSection)
		CELL_DELETE mHeadSection;

	if (mTail)
		CELL_DELETE mTail;
	if (mTailSection)
		CELL_DELETE mTailSection;

	EntryMap::iterator iter, end = mEntries.end();
	for (iter = mEntries.begin(); iter != end; ++iter)
	{
		WorkChain::Entry* entry = iter->second;
		CELL_DELETE entry;
	}
}
//-----------------------------------------------------------------------
WorkChain::Entry* WorkChain::getEntry(const String& name)
{
	EntryMap::const_iterator iter = mEntries.find(name);
	if (iter != mEntries.end())
	{
		return iter->second;
	}
	return NULL;
}
//-----------------------------------------------------------------------
Section* WorkChain::get(const String& name)
{
	WorkChain::Entry* entry = getEntry(name);
	if (entry)
	{
		return entry->getSection();
	}
	return NULL;
}
//-----------------------------------------------------------------------
void WorkChain::append(Section* section)
{
	addLast(section);
}
//-----------------------------------------------------------------------
void WorkChain::addFirst(Section* section)
{
	if (checkAddable(section->getName()))
		addEntry(mHead, section);
}
//-----------------------------------------------------------------------
void WorkChain::addLast(Section* section)
{
	if (checkAddable(section->getName()))
		addEntry(mTail->mPrevEntry, section);
}
//-----------------------------------------------------------------------
void WorkChain::addBefore(Section* base, Section* section)
{
	addBefore(base->getName(), section);
}
//-----------------------------------------------------------------------
void WorkChain::addBefore(const String& baseName, Section* section)
{
	if (checkAddable(section->getName()))
	{
		EntryMap::const_iterator iter = mEntries.find(baseName);
		if (iter != mEntries.end())
		{
			Entry* baseEntry = iter->second;
			addEntry(baseEntry->mPrevEntry, section);
		}
	}
}
//-----------------------------------------------------------------------
void WorkChain::addAfter(Section* base, Section* section)
{
	addAfter(base->getName(), section);
}
//-----------------------------------------------------------------------
void WorkChain::addAfter(const String& baseName, Section* section)
{
	if (checkAddable(section->getName()))
	{
		EntryMap::const_iterator iter = mEntries.find(baseName);
		if (iter != mEntries.end())
		{
			Entry* baseEntry = iter->second;
			addEntry(baseEntry, section);
		}
	}
}
//-----------------------------------------------------------------------
void WorkChain::remove(const String& name)
{
	removeEntry(name);
}
//-----------------------------------------------------------------------
Section* WorkChain::replace(const String& name, Section* newSection)
{
	EntryMap::const_iterator iter = mEntries.find(name);
	if (iter != mEntries.end())
	{
		Entry* entry = iter->second;
		Section* oldSection = entry->getSection();
		entry->mSection = newSection;
		return oldSection;
	}
	return NULL;
}
//-----------------------------------------------------------------------
bool WorkChain::exists(const String& name)
{
	return (!checkAddable(name));
}
//-----------------------------------------------------------------------
void WorkChain::fire(void)
{
	mDone = false;

	Section* section = mHead->getSection();
	NextSection* nextSection = mHead->getNextSection();
	section->relayed(nextSection);
}
//-----------------------------------------------------------------------
bool WorkChain::checkAddable(const String& name)
{
	EntryMap::const_iterator iter = mEntries.find(name);
	if (iter == mEntries.end())
		return true;
	return false;
}
//-----------------------------------------------------------------------
void WorkChain::addEntry(Entry* prevEntry, Section* section)
{
	Entry* newEntry = CELL_NEW Entry(this, prevEntry, prevEntry->mNextEntry, section);
	prevEntry->mNextEntry->mPrevEntry = newEntry;
	prevEntry->mNextEntry = newEntry;
	mEntries.insert(EntryMap::value_type(section->getName(), newEntry));
}
//-----------------------------------------------------------------------
void WorkChain::removeEntry(const String& name)
{
	EntryMap::iterator iter = mEntries.find(name);
	if (iter != mEntries.end())
	{
		Entry* entry = iter->second;
		Entry* prevEntry = entry->mPrevEntry;
		Entry* nextEntry = entry->mNextEntry;
		prevEntry->mNextEntry = nextEntry;
		nextEntry->mPrevEntry = prevEntry;
		mEntries.erase(iter);
		CELL_DELETE entry;
	}
}


//-----------------------------------------------------------------------
WorkChain::Entry::Entry(WorkChain* chain,
	Entry* prevEntry, Entry* nextEntry, Section* section)
	: mPrevEntry(prevEntry)
	, mNextEntry(nextEntry)
	, mSection(section)
	, mNextSection(NULL)
{
	mNextSection = CELL_NEW NextSection(chain, this);
}
//-----------------------------------------------------------------------
WorkChain::Entry::~Entry(void)
{
	if (mNextSection)
	{
		CELL_DELETE mNextSection;
		mNextSection = NULL;
	}
}
//-----------------------------------------------------------------------
Section* WorkChain::Entry::getSection(void)
{
	return mSection;
}
//-----------------------------------------------------------------------
NextSection* WorkChain::Entry::getNextSection(void)
{
	return mNextSection;
}


//-----------------------------------------------------------------------
NextSection::NextSection(void)
	: mChain(NULL)
	, mEntry(NULL)
{
}
//-----------------------------------------------------------------------
NextSection::NextSection(WorkChain* chain, WorkChain::Entry* entry)
	: mChain(chain)
	, mEntry(entry)
{
}
//-----------------------------------------------------------------------
NextSection::~NextSection(void)
{
}
//-----------------------------------------------------------------------
void NextSection::relay(void)
{
	WorkChain::Entry* entry = mEntry->mNextEntry;
	mChain->mCurrentEntry = entry;
	NextSection* nextSection = entry->getNextSection();
	Section* section = entry->getSection();
	section->relayed(nextSection);
}


//-----------------------------------------------------------------------
HeadSection::HeadSection(WorkChain* chain)
	: Section("_head_", chain)
{
}
//-----------------------------------------------------------------------
HeadSection::~HeadSection(void)
{
}
//-----------------------------------------------------------------------
void HeadSection::relayed(NextSection* nextSection)
{
	nextSection->relay();
}


//-----------------------------------------------------------------------
TailSection::TailSection(WorkChain* chain)
	: Section("_tail_", chain)
{
}
//-----------------------------------------------------------------------
TailSection::~TailSection(void)
{
}
//-----------------------------------------------------------------------
void TailSection::relayed(NextSection* nextSection)
{
	getChain()->mDone = true;
}

} // end namespace cell
